# -*- coding: utf-8 -*-
import pyvisa as visa
from pyvisa import constants


class Block(object):
    def __init__(self, inst_obj):
        self.__data = 0
        self.__get_cmd = ''
        self.__set_cmd = ''
        self.__inst_obj = inst_obj

    @property
    def data(self):
        print('inst read %d' % self.__data)
        cmd = '%s\n' % (self.__get_cmd)
        data_str = self.__inst_obj.query(cmd)
        return self.__data

    @data.setter
    def data(self, data):
        print('inst write %d' % data)
        cmd = '%s %g' % (self.__set_cmd, data)
        self.__inst_obj.write(cmd)
        self.__data = data


class VisaInst(object):
    inst_obj = None

    def __init__(self, visa_address):
        self.visa_address = visa_address
        rm = visa.ResourceManager()
        # print('open instrument')
        self.inst_obj = rm.open_resource(self.visa_address)
        self.inst_obj.set_visa_attribute(constants.VI_ATTR_TERMCHAR_EN, constants.VI_TRUE)
        self.inst_name = self.inst_obj.query("*IDN?")
        return

    def __del__(self):
        if self.inst_obj:
            self.inst_obj.close()
        return

    def getInstName(self):
        return self.inst_name

    def printInstName(self):
        print(self.inst_name)
        return
